import 'dart:math';

import 'package:color_thief_flutter/color_thief_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:logger/logger.dart';

Future getColor(String url) {
  //获取图片颜色
  return getColorFromUrl(url).then((color) {
    HSLColor hslColor =
        HSLColor.fromColor(Color.fromARGB(255, color[0], color[1], color[2]));
    final hue = max(hslColor.hue - 11, 0);
    final saturation = min(hslColor.saturation + 0.13, 1.0);
    final lightness = min(hslColor.lightness + 0.04, 1.0);
    print('saturation$saturation,hue$hue,lightness$lightness');
    hslColor = hslColor.withHue(hue); //色调
    hslColor = hslColor.withSaturation(saturation); //饱和度
    hslColor = hslColor.withLightness(lightness); //亮度
    return hslColor.toColor();
  });
}

getFadeColor(Color color) {
  //获取淡一点的颜色
  HSLColor hslColor = HSLColor.fromColor(color);
  final hue = min(hslColor.hue, 360.0);
  final saturation = max(hslColor.saturation - 0.06, 0);
  final lightness = min(hslColor.lightness + 0.07, 1.0);
  // print('saturation$saturation,hue$hue,lightness$lightness');
  hslColor = hslColor.withHue(hue); //色调
  hslColor = hslColor.withSaturation(saturation); //饱和度
  hslColor = hslColor.withLightness(lightness); //亮度
  return hslColor.toColor();
}

countFilter(int val) {
  //数量过滤
  return val != null ? val.toString() : '0';
}

showToast(String msg) {
  Fluttertoast.showToast(
    msg: msg,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIosWeb: 1,
  );
}

//邮箱验证
bool isEmail(String str) {
  return RegExp(
          r'^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$')
      .hasMatch(str);
}

var logger = Logger();